package com.xigua._树;

import com.xigua.TreeNode;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/3021:52
 */
public class _701_二叉搜索树中的插入操作 {

    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return  new TreeNode(val,null,null);
        TreeNode node = root;
        while (node != null){
                if (val < node.val) {
                    if (node.left == null) {
                        node.left = new TreeNode(val);
                        break;
                    } else {
                        node = node.left;
                    }
                } else {
                    if (node.right == null) {
                        node.right = new TreeNode(val);
                        break;
                    } else {
                        node = node.right;
                    }
                }
        }
        return root;
    }
}
